<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login-form"
          :model="loginFrom"
          :rules="rules"
          ref="ruleFormRef"
        >
          <h1>Hello</h1>
          <h2>欢迎来到鬼谷子</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginFrom.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              show-password
              v-model="loginFrom.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="default"
              class="loginBtn"
              @click="login(ruleFormRef)"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import useUserStore from '@/store/user/user.ts'
import { getTime } from '@/utils/time.ts'
import type { FormInstance, FormRules } from 'element-plus'
const $router = useRouter()
const userStore = useUserStore()
const ruleFormRef = ref<FormInstance>()

const usernameValidator = (rule: any, value: any, callback: any) => {
  console.log(value)

  if (/^\S{5,10}$/.test(value)) {
    callback()
  } else {
    callback(new Error('账号长度至少五位'))
  }
}

const passwordValidator = (rule: any, value: any, callback: any) => {
  if (/^\d{6,10}$/.test(value)) {
    callback()
  } else {
    callback(new Error('密码长度至少六位'))
  }
}

const rules = reactive<FormRules<RuleForm>>({
  username: [
    // { required: true, message: 'Please input Activity username', trigger: 'blur' },
    // { min: 3, max: 6, message: 'Length should be 3 to 6', trigger: 'change' },
    { validator: usernameValidator, trigger: 'change' },
  ],
  password: [
    // { required: true, message: 'Please input Activity password', trigger: 'blur' },
    // { min: 3, max: 6, message: 'Length should be 3 to 6', trigger: 'change' },
    { validator: passwordValidator, trigger: 'change' },
  ],
})
interface RuleForm {
  username: string
  password: string
}

let loading = ref<Boolean>(false)
let loginFrom = reactive<RuleForm>({
  username: '',
  password: '',
})
const login = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      loading.value = true
      try {
        await userStore.userLogin(loginFrom)
        $router.push('/')
        ElNotification({
          type: 'success',
          message: '欢迎回来',
          title: `Hi,${getTime()}好`,
        })
        loading.value = false
      } catch (e) {
        loading.value = false
        return ElNotification({
          type: 'error',
          message: (e as Error).message,
        })
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

onMounted(() => {})
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login-form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }

    .loginBtn {
      width: 100%;
    }
  }
}
</style>
